package com.example.home_stretch;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import org.litepal.LitePal;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Landing_registration extends AppCompatActivity {
    @BindView(R.id.ed_user)
    EditText edUser;
    @BindView(R.id.ed_password)
    EditText edPassword;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.retrieve_password)
    TextView retrievePassword;
    @BindView(R.id.bu_sign_in)
    Button buSignIn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_landing_registration);
        ButterKnife.bind(this);
        initsign();
    }

    private void initsign() {
        List<Booksign> all = LitePal.findAll(Booksign.class);
        if (all.size() > 1) {
            Toast.makeText(this, "登录成功，欢迎回来！", Toast.LENGTH_SHORT).show();
        }
    }

    private void initdata() {
        LitePal.getDatabase();
        Booksign booksign = new Booksign();
        booksign.setUser(edUser.getText().toString());
        booksign.setPassword(edPassword.getText().toString());
        booksign.save();
        List<Booksign> all = LitePal.findAll(Booksign.class);
        for (Booksign booksign1 : all) {
            Log.i("TAG", "initdataa丨" + booksign1.getUser());
            Log.i("TAG", "initdataa丨" + booksign1.getPassword());
            Log.i("TAG", "initdataa丨" + booksign1.getMailbox());
        }
    }

    private void initview() {
        Booksign lastbook = LitePal.findLast(Booksign.class);
        Log.i("TAG", "initviewbbb" + lastbook.getUser());
        Log.i("TAG", "initviewbbb" + lastbook.getPassword());
        Log.i("TAG", "initviewbbb" + lastbook.getMailbox());
        if (edUser.getText().toString().equals(lastbook.getUser()) && edPassword.getText().toString().equals(lastbook.getPassword())) {
            Toast.makeText(this, "登录成功，欢迎回来！", Toast.LENGTH_SHORT).show();
            initdata();
        } else if (edUser.getText().toString().equals("") || edPassword.getText().toString().equals("")) {
            Toast.makeText(this, "请输入账号密码", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "账号或者密码错误", Toast.LENGTH_SHORT).show();
        }
    }

    @OnClick(R.id.bu_sign_in)
    public void onViewClicked() {
        initview();
    }

    @OnClick(R.id.tv_register)
    public void onViewClicked1() {
        AlertDialog.Builder Dialog = new AlertDialog.Builder(this);
        View dialogView = LayoutInflater.from(this).inflate(R.layout.activity_landing_two, null);
        ViewHolder viewHolder = new ViewHolder(dialogView);
        Dialog.setView(dialogView);
        //点击外部消失弹窗
        Dialog.setCancelable(true);
        AlertDialog customAlert = Dialog.show();
        //设置自定义逻辑
        viewHolder.buSubmissionOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Booksign booksign = new Booksign();
                if (viewHolder.edUserOne.getText().toString().length() >= 4) {
                    booksign.setUser(viewHolder.edUserOne.getText().toString().trim());
                } else {
                    Toast.makeText(Landing_registration.this, "用户名至少要4位字母，请重新设置", Toast.LENGTH_SHORT).show();
                }
                if (viewHolder.edMailboxOne.getText().toString().length() >= 6) {
                    booksign.setMailbox(viewHolder.edMailboxOne.getText().toString().trim());
                } else {
                    Toast.makeText(Landing_registration.this, "邮箱至少6位数字", Toast.LENGTH_SHORT).show();
                }
                if (viewHolder.edPasswordOne.getText().toString().length() >= 6 && viewHolder.edPasswordTwo.getText().toString().length() >= 6) {
                    booksign.setPassword(viewHolder.edPasswordTwo.getText().toString().trim());
                } else {
                    Toast.makeText(Landing_registration.this, "密码至少6位数", Toast.LENGTH_SHORT).show();
                }
//                if (viewHolder.edPasswordOne.getText().toString().equals(viewHolder.edPasswordTwo.toString())){
//                    booksign.setPassword(viewHolder.edPasswordTwo.getText().toString());
//                }else{
//                    Toast.makeText(Landing_registration.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
//                    return;
//                }
                booksign.save();
                Toast.makeText(Landing_registration.this, "提交成功", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @OnClick(R.id.retrieve_password)
    public void onViewClicked3() {
        AlertDialog.Builder Dialog1 = new AlertDialog.Builder(this);
        View dialogView1 = LayoutInflater.from(this).inflate(R.layout.activity_landing_three, null);
        ViewHolder2 viewHolder2 = new ViewHolder2(dialogView1);
        Dialog1.setView(dialogView1);
        Dialog1.setCancelable(true);
        AlertDialog cust = Dialog1.show();
        viewHolder2.buSubmissionTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //自定义逻辑
                Booksign lastbook = LitePal.findLast(Booksign.class);
                Log.i("TAG", "initviewbbb" + lastbook.getUser());
                Log.i("TAG", "initviewbbb" + lastbook.getMailbox());
                Log.i("TAG", "initviewbbb" + lastbook.getPassword());
                if (viewHolder2.edUserTwo.getText().toString().equals(lastbook.getUser())&&viewHolder2.edMailboxTwo.getText().toString().equals(lastbook.getMailbox())) {
                    Toast.makeText(Landing_registration.this, "您的密码为"+lastbook.getPassword(), Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(Landing_registration.this, "账号或邮箱填写错误，请重新输入", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    static
    class ViewHolder {
        @BindView(R.id.ed_user_one)
        EditText edUserOne;
        @BindView(R.id.ed_mailbox_one)
        EditText edMailboxOne;
        @BindView(R.id.ed_password_one)
        EditText edPasswordOne;
        @BindView(R.id.ed_password_two)
        EditText edPasswordTwo;
        @BindView(R.id.bu_submission_one)
        Button buSubmissionOne;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    class ViewHolder2 {
        @BindView(R.id.ed_user_two)
        EditText edUserTwo;
        @BindView(R.id.ed_mailbox_two)
        EditText edMailboxTwo;
        @BindView(R.id.bu_submission_two)
        Button buSubmissionTwo;

        ViewHolder2(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
